package com.seaboxdata.dao;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.security.UserGroupInformation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URI;

/**
 * @author
 * @create 2020-12-30 16:20
 **/
@Component
public class HdfsDao {

    @Value("${hdfs.url}")
    private String hdfsUrl;

    @Value("${hdfs.principal}")
    private String principal;

    @Value("${krb5.path}")
    private String krb5Path;

    @Value("${hdfs.keytab.path}")
    private String keyTabPath;

    @Value("${hive.database}")
    private String database;

    @Value("${hive.base.path}")
    private String hiveBasePath;

    private FileSystem fs;

    public FileSystem getFileSystem() throws IOException {
        if (fs == null) {
            Configuration conf = new Configuration();
            if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
                this.krb5Path = "F:/dfjx/keytab/krb5.conf";
                this.keyTabPath = "F:/dfjx/keytab/hdfs.headless.keytab";
            }
            System.setProperty("java.security.krb5.conf", krb5Path);
            System.setProperty("sun.security.krb5.debug", "false");
            conf.set("hadoop.security.authentication", "Kerberos");
            UserGroupInformation.setConfiguration(conf);
            conf.set("keytab.file", keyTabPath);
            conf.set("kerberos.principal", principal);
            try {
                UserGroupInformation.loginUserFromKeytab(principal, keyTabPath);
            } catch (IOException e) {
                e.printStackTrace();
            }
            fs = FileSystem.get(URI.create(hdfsUrl), conf);
        }
        return fs;
    }





}
